package com.emep.changzhi.analyse.service.statistics;

import com.emep.changzhi.analyse.entity.aqi.DataAqiInfo;
import com.emep.changzhi.analyse.repository.aqi.DataAqiInfoRepository;
import com.emep.changzhi.analyse.service.outlet.SysOutletInfoService;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import com.emep.changzhi.analyse.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * 根据起始时间和污染物名称来获取国控站点信息
 */
@Service
public class GetDayPositionNameService {

    @Autowired
    private DataAqiInfoRepository dataAqiInfoRepository;
    private static final Logger logger = LoggerFactory.getLogger(SysOutletInfoService.class);


    public Object findapiByTime(Map<String, Object> payload) {
        List<DataAqiInfo> dataAqiInfoList = new ArrayList<>();
        DataAqiInfo dataAqiInfo=new DataAqiInfo();
        List list = new ArrayList<>();
        Set set = new HashSet();
        //行政区划
        String 	cityCode = payload.get("cityCode") != null ? payload.get("cityCode").toString() : "";
        //开始时间
        String startTime =String.valueOf( payload.get("startTime")!= null ? payload.get("startTime").toString():"");
        //结束时间
        String endTime =String.valueOf( payload.get("endTime")!= null ? payload.get("endTime").toString():"");
        //污染物名称
        String pollutionName = payload.get("pollutionName")!= null ? payload.get("pollutionName").toString():"";
        try {
        if (!StringUtils.isEmpty(cityCode) && !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)&& !StringUtils.isEmpty(pollutionName)) {
            //开始时间
            String start=String.valueOf( DateTimeUtil.stringToDate(startTime,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());
            //结束时间
            String end= String.valueOf( DateTimeUtil.stringToDate(endTime,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());
            dataAqiInfoList = dataAqiInfoRepository.findAll(new Specification<DataAqiInfo>() {
                @Override
                public javax.persistence.criteria.Predicate toPredicate(Root<DataAqiInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                    javax.persistence.criteria.Predicate p1 = null;
                    javax.persistence.criteria.Predicate p2 = null;
                    javax.persistence.criteria.Predicate p3 = null;
                    //大于或等于传入时间
                    p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("timePoint").as(String.class), start);
                    //小于于或等于传入时间
                    p2 = criteriaBuilder.lessThanOrEqualTo(root.get("timePoint").as(String.class), end);
                    //行政区划
                    p3 = criteriaBuilder.equal(root.get("areaId").as(String.class), cityCode);
                    query.where(criteriaBuilder.and(p1, p2, p3));
                    return query.getRestriction();
                }
            });
        }
      for(int i = 0 ; i < dataAqiInfoList.size() ; i++) {
            dataAqiInfo=dataAqiInfoList.get(i);
          list.add(dataAqiInfo.getPositionName());

        }
            set.addAll(list);

        } catch (Exception e)

        {
            logger.error(e.getLocalizedMessage());
        }
        return  set;


    }
}
